﻿using Hopeless.Metaphysical;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCS.XNA.Utils;
using UCS.XNA.Utils.Animation;
using UCS.XNA.Utils.Geometries;
using UCS.XNA.Utils.Physics;

namespace Hopeless.Physical
{
    class Abyss
    {
        private Rectangle _space;
        public Rectangle Space
        {
            get { return _space; }
        }

        private Texture2D _texture;

        public void Initialize(Rectangle availableSpace)
        {
            _space = availableSpace;
            _texture = GameResources.Content.Load<Texture2D>("Background_Abyss");
        }

        public void Update(GameTime gameTime)
        {
            var zombies = HopelessGame.Universe.CurrentWave.SpawnedZombies;
            foreach (var zombie in zombies)
            {
                if (_space.Contains((int)zombie.Position.X, (int)zombie.Position.Y) && !zombie.Falling)
                {
                    zombie.Falling = true;
                    var scaleAnimation = new ScaleAnimation(zombie, 0f, 0.02f);
                    scaleAnimation.AnimationFinished += (sender, e) => zombie.TakeDamage(999999999);

                    Physics.ScaleAnimations.Add(scaleAnimation);
                }
            }

            var hero = HopelessGame.Universe.Hero;
            if (hero == null)
                return;

            if (_space.Contains((int)hero.Position.X, (int)hero.Position.Y) && !hero.IsDead && !hero.Falling)
            {
                hero.Falling = true;
                var heroScaleAnimation = new ScaleAnimation(hero, 0f, 0.02f);
                var swordScaleAnimation = new ScaleAnimation(hero.Sword, 0f, 0.02f);
                heroScaleAnimation.AnimationFinished += (sender, e) => hero.TakeDamage(999999999);

                Physics.ScaleAnimations.Add(heroScaleAnimation);
                Physics.ScaleAnimations.Add(swordScaleAnimation);
            }

            foreach (var bloodSpill in HopelessGame.Universe.BloodSpills)
            {
                if (_space.Contains((int)bloodSpill.Position.X, (int)bloodSpill.Position.Y) && !bloodSpill.Falling)
                {
                    bloodSpill.Falling = true;
                    var scaleAnimation = new ScaleAnimation(bloodSpill, 0f, 0.02f);
                    Physics.ScaleAnimations.Add(scaleAnimation);
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            GameResources.SpriteBatch.Begin();
            GameResources.SpriteBatch.Draw(_texture, _space, Color.White);
            GameResources.SpriteBatch.End();
        }
    }
}
